#include "stdafx.h"
#include "../resource.h"
#include "FrameWin32.h"
#include "../app/App.h"
//#include "Editor.h"
#include "../ui/Menu.h"

#include "ButtonWin32.h"

HWND FrameWin32::Create(int x, int y, int width, int height){
	static bool registed = false;
	static const wchar_t* clsName = L"myFrame";
	if(!registed){
		WNDCLASSEXW wndcls = {0};
		wndcls.cbSize= sizeof(WNDCLASSEX);
		wndcls.style = CS_HREDRAW | CS_VREDRAW;
		wndcls.lpfnWndProc = &WinMsg::StaticWndProc;
		wndcls.cbClsExtra = 0;
		wndcls.cbWndExtra = sizeof(void*);
		wndcls.hInstance = GetModuleHandle(0);
		wndcls.hbrBackground = (HBRUSH)(COLOR_WINDOW +1);
		wndcls.hCursor = LoadCursor(NULL, IDC_ARROW);
		wndcls.hIcon = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_JAC));
		wndcls.hIconSm = LoadIcon(GetModuleHandle(0), MAKEINTRESOURCE(IDI_SMALL));
		wndcls.lpszClassName = clsName;
		wndcls.lpszMenuName =0;
		registed= RegisterClassEx(&wndcls) != 0;
	}
	if(!registed){
		return 0;
	}
	return ::CreateWindowEx(0, clsName, L"", 
		WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS, 
		x, y, width, height, 0, 0, GetModuleHandle(0), (WinBase*)this);

}

FrameWin32::FrameWin32(){
}
MSGPROC(FrameWin32::OnCreate){
	InitMenu();
	CreateChildren();
	LayoutChildren();

	//ButtonWin32* button = 0;
	//button = new ButtonWin32();
	//button->SetText(L"Hello");
	//button->Create(GetWnd());

	
	//HWND w = button->GetUIHand();
	//w = button->GetWnd();;
	//BIND_MSG_MEM(w, WM_LBUTTONDOWN, &FrameWin32::OnButtonClick, this);
	return true;
}
MSGPROC(FrameWin32::OnButtonClick){
	::MessageBox(0, L"Hello", L"Test", MB_OK);
	MSG_HANDLED;
	return 0;
}
//Editor* FrameWin32::GetEditor(){
//	return &editor_;
//}
void FrameWin32::InitMenu(){
	menu_.reset(new Menu(GetWnd()));
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.new"), &FrameWin32::OnFileNew, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.open"), &FrameWin32::OnFileOpen, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.save"), &FrameWin32::OnFileSave, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.saveas"), &FrameWin32::OnFileSaveAs, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.saveall"), &FrameWin32::OnFileSaveAll, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"file.exit"), &FrameWin32::OnFileExit, this);

	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.copy"), &FrameWin32::OnEditCopy, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.cut"), &FrameWin32::OnEditCut, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.paste"), &FrameWin32::OnEditPaste, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.undo"), &FrameWin32::OnEditUndo, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"edit.redo"), &FrameWin32::OnEditRedo, this);
	BIND_CMD_MEM(GetWnd(), KeyID::GetID(L"help.about"), &FrameWin32::OnHelpAbout, this);
}
void FrameWin32::LayoutChildren(){
	
}
void FrameWin32::CreateChildren(){
	
}

void FrameWin32::OnBindMsg(){
	BIND_MSG_MEM(GetWnd(), WM_CREATE, &FrameWin32::OnCreate, this );
	BIND_MSG_MEM(GetWnd(), WM_PAINT, &FrameWin32::OnPaint, this );
	BIND_MSG_MEM(GetWnd(), WM_DESTROY, &FrameWin32::OnDestroy, this );
	BIND_MSG_MEM(GetWnd(), WM_SIZE, &FrameWin32::OnSize, this );
	BIND_MSG_MEM(GetWnd(), WM_SETFOCUS, &FrameWin32::OnFocus, this );
	BIND_MSG_MEM(GetWnd(), WM_COMMAND, &FrameWin32::OnCommand, this );

}

MSGPROC(FrameWin32::OnFocus){

	MSG_HANDLED;
	return 0;
}

MSGPROC(FrameWin32::OnFileNew){
	//GetEditor()->NewFile();
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnFileOpen){
	/*vector<wstring> files = util::OpenFileDialog(GetWnd(), true);
	foreach(wstring& f, files){
		GetEditor()->OpenFile(f);
	}*/
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnFileSave){
	//GetEditor()->SaveFile();
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnFileSaveAs){
	/*wstring filename = util::SaveFileDialog(GetWnd());
	if(!filename.empty()){
		GetEditor()->SaveAsFile(filename);
	}*/
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnFileSaveAll){
	//GetEditor()->SaveAllFile();
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnFileExit){
	DestroyWindow();
	MSG_HANDLED;
	return 0;
}

MSGPROC(FrameWin32::OnEditCopy){
	util::dbg(L"EditCopy");
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnEditCut){
	util::dbg(L"EditCut");
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnEditPaste){
	util::dbg(L"EditPaste");
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnEditUndo){
	util::dbg(L"EditUndo");
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnEditRedo){
	util::dbg(L"EditRedo");
	MSG_HANDLED;
	return 0;
}

MSGPROC(FrameWin32::OnHelpAbout){
	MessageBox(0, L"about", 0, 0);
	return 0;
}
MSGPROC(FrameWin32::OnSize){
	LayoutChildren();
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnPaint){
	PAINTSTRUCT ps = {0};
	HDC hdc = BeginPaint(hWnd, &ps);

	EndPaint(hWnd, &ps);
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnDestroy){
	App::Exit();
	MSG_HANDLED;
	return 0;
}
MSGPROC(FrameWin32::OnCommand){
	//SendMessage(GetEditor()->GetWnd(), uMsg, wParam, lParam);
	return 0;
}

